package org.darkforest.merchant.init;

import org.darkforest.common.services.SettingService;
import org.darkforest.common.util.AliOssFileUploadUtil;
import org.darkforest.common.util.SystemConstant;
import org.darkforest.enums.SettingNameEnum;
import org.darkforest.model.Menu;
import org.darkforest.vo.SiteSettingInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenyu on 15/1/29.
 * 启动项目时加载动态菜单数据，暂时存入applicationContext中，后续改为读取数据库数据或存储在redis中
 */
@Service
public class MenuInfoLoad implements ServletContextAware {

    Logger logger = LoggerFactory.getLogger(MenuInfoLoad.class);

    @Autowired
    private SettingService settingService;

    /**
     * 启动时加载菜单列表
     * @param application
     */
    @Override
    public void setServletContext(ServletContext application) {
//        System.out.println("initing....");
        application.setAttribute("menuList", getSubMenuList());
        application.setAttribute("pmenuList", getParentMenuList());
//        System.out.println("menuList put in application,list:" + ((List)application.getAttribute("menuList")).size());

        //对图片上传类赋值参数
//        SpringContextHolder contextHolder = SpringContextHolder.getContextHolder();
//        SettingService settingService = (SettingService)contextHolder.getContext().getBean("settingServiceImpl");
        String alioss_accessId = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ACCESS_ID);
        logger.debug("accessId:"+alioss_accessId);
        AliOssFileUploadUtil.accessId = alioss_accessId;
        AliOssFileUploadUtil.accessKey = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ACCESS_KEY);
//        AliOssFileUploadUtil.endPoint = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ENDPOINT);
        AliOssFileUploadUtil.endPoint = settingService.getSettingInfo(SettingNameEnum.IMAGE_UPLOAD_ENDPOINT);
        AliOssFileUploadUtil.bucketName = settingService.getSettingInfo(SettingNameEnum.ALIOSS_BUCKETNAME);
//        SystemConstant.ALIOSS_PATH = "http://" + AliOssFileUploadUtil.bucketName + ".oss-cn-beijing.aliyuncs.com";
        SystemConstant.ALIOSS_PATH = settingService.getSettingInfo(SettingNameEnum.IMAGE_SHOW_ENDPOINT);
        SystemConstant.PCWEB_PATH = settingService.getSettingInfo(SettingNameEnum.PCWEBSITE);
        SystemConstant.PCWEB_GOODS_DETAIL_PATH = SystemConstant.PCWEB_PATH + "/goods/detail?id=";
        SystemConstant.PCWEB_GOODSCOMM_DETAIL_PATH= SystemConstant.PCWEB_PATH + "/goods/detailByComm?commId=";
        logger.debug("aliossPath:"+SystemConstant.ALIOSS_PATH);
        SiteSettingInfo siteSettingInfo = new SiteSettingInfo();
        //读取数据库配置加载到对象
        settingService.initSiteSetting(siteSettingInfo);
    }

    /**
     * 获取一级菜单列表
     * @return
     */
    private static List<Menu> getParentMenuList(){
        List<Menu> pmenu = new ArrayList<Menu>();
        //首页
        Menu pm1 = new Menu(10,"首页","main","ico-index");
        Menu pm2 = new Menu(20,"商品","goods/goodsAdd","ico-goods");
        Menu pm3 = new Menu(30,"订单物流","order/orderList","ico-order");
        Menu pm4 = new Menu(40,"促销","activeArea/activeAreaGoodsList","ico-promotion");
//        Menu pm5 = new Menu(50,"售后服务","customer/refundList","ico-consult");
        Menu pm6 = new Menu(60,"统计计算","stat/profile","ico-statistics");
//        Menu pm7 = new Menu(70,"消息","callCenter/system","ico-message");
//        Menu pm8 = new Menu(80,"会员","member/memList","ico-member");
        Menu pm9 = new Menu(90,"系统设置","account/accountList","ico-setting");
        Menu pm10 = new Menu(100,"店铺","store/view","ico-store");

        pmenu.add(pm1);
        pmenu.add(pm2);
        pmenu.add(pm3);
        pmenu.add(pm4);
//        pmenu.add(pm5);
        pmenu.add(pm6);
//        pmenu.add(pm7);
//        pmenu.add(pm8);
        pmenu.add(pm9);
        pmenu.add(pm10);

        return pmenu;
    }

    /**
     * 子菜单列表
     * @return
     */
    private static List<List<Menu>>  getSubMenuList(){
        //商品子菜单
        Menu sm201 = new Menu(201,"商品发布","goods/goodsAdd");
        Menu sm202 = new Menu(202,"出售中的商品","goods/goodsListOnline");
        Menu sm203 = new Menu(203,"仓库中的商品","goods/goodsListOffline");
//        Menu sm204 = new Menu(204,"礼品包装","giftpackage/list");
//        Menu sm205 = new Menu(205,"定制配送","giftdistribution/list");
        Menu sm206 = new Menu(206,"图片空间","album/list");
//        Menu sm207 = new Menu(207,"商品分类","goods/gclass");
//        Menu sm208 = new Menu(208,"规格管理","spec/specList");
//        Menu sm209 = new Menu(209,"标签管理","tag/tagList");
//        Menu sm210 = new Menu(210,"礼品卡片","giftcardtemplate/list");
//        Menu sm211 = new Menu(211,"品牌管理","goods/brand");

        //订单物流子菜单
        Menu sm31 = new Menu(31,"实物交易订单","order/orderList");
        Menu sm32 = new Menu(32,"发货","order/deliverList");
        Menu sm33 = new Menu(33,"发货设置","deliver/deliverSetList");
        Menu sm34 = new Menu(34,"运单模板","module/moduleList");
//        Menu sm35 = new Menu(35,"物流工具","transport/transportList");
//        Menu sm36 = new Menu(36,"礼品领取记录","giftCode/giftCodeList");

        //促销子菜单
//        Menu sm41 = new Menu(41,"推荐展位","promotion/recommend");
//        Menu sm42 = new Menu(42,"活动","speciality/specialityList");
        Menu sm43 = new Menu(43,"专区推荐","activeArea/activeAreaGoodsList");

        //售后服务子菜单
//        Menu sm51 = new Menu(51,"退款记录","customer/refundList");
//        Menu sm52 = new Menu(52,"退货记录","customer/rebackList");
//        Menu sm53 = new Menu(53,"退货退款原因","customer/reasonList");

        //统计计算子菜单
        Menu sm61 = new Menu(61,"统计概览","stat/profile");
        Menu sm62 = new Menu(62,"商品分析","stat/goods");
//        Menu sm63 = new Menu(63,"流量统计","stat/flow");
//        Menu sm64 = new Menu(64,"供货渠道","stat/supplyChannel");
//        Menu sm65 = new Menu(65,"会员消费统计","stat/memberConsumpStatistic");

        //客服消息子菜单
//        Menu sm71 = new Menu(71,"系统消息","callCenter/system");
//        Menu sm72 = new Menu(72,"发送消息","notice/toNoticeSend");
//        Menu sm73 = new Menu(73,"消息模板管理","notice/noticeModelList");

        //会员子菜单
//        Menu sm81 = new Menu(81,"会员管理","member/memList");
//        Menu sm82 = new Menu(82,"会员级别","member/levelList");
//        Menu sm83 = new Menu(83,"预存款","member/positSetting");
//        Menu sm84 = new Menu(84,"积分管理","member/pointsList");

        //系统设置子菜单
//        Menu sm91 = new Menu(91,"参数设置","enumParam/list");
        Menu sm92 = new Menu(92,"帐号列表","account/accountList");
        Menu sm93 = new Menu(93,"帐号组","account/groupList");

        // 店铺子菜单
        Menu sm101 = new Menu(101,"店铺","store/view");

        List sl_1 = new ArrayList<Menu>(); //首页
        List sl_2 = new ArrayList<Menu>(); //商品
        List sl_3 = new ArrayList<Menu>(); //订单物流
        List sl_4 = new ArrayList<Menu>(); //促销
//        List sl_5 = new ArrayList<Menu>();//售后服务
        List sl_6 = new ArrayList<Menu>(); //统计计算
//        List sl_7 = new ArrayList<Menu>(); //客服消息
//        List sl_8 = new ArrayList<Menu>(); //会员
        List sl_9 = new ArrayList<Menu>(); //系统设置
        List sl_10 = new ArrayList<Menu>(); //店铺

        List menuList = new ArrayList<List<Menu>>();

        //首页
        // 无

        //商品
        sl_2.add(sm201);
        sl_2.add(sm202);
        sl_2.add(sm203);
//        sl_2.add(sm204);
//        sl_2.add(sm205);
        sl_2.add(sm206);
//        sl_2.add(sm207);
//        sl_2.add(sm208);
//        sl_2.add(sm209);
//        sl_2.add(sm210);
//        sl_2.add(sm211);

        //订单物流
        sl_3.add(sm31);
        sl_3.add(sm32);
        sl_3.add(sm33);
        sl_3.add(sm34);
//        sl_3.add(sm35);
//        sl_3.add(sm36);

        // 促销
//        sl_4.add(sm41);
//        sl_4.add(sm42);
        sl_4.add(sm43);

        // 售后服务
//        sl_5.add(sm51);
//        sl_5.add(sm52);
//        sl_5.add(sm53);

        // 统计计算
        sl_6.add(sm61);
        sl_6.add(sm62);
//        sl_6.add(sm63);
//        sl_6.add(sm64);
//        sl_6.add(sm65);

            //客服消息
//        sl_7.add(sm71);
//        sl_7.add(sm72);
//        sl_7.add(sm73);

        //会员
//        sl_8.add(sm81);
//        sl_8.add(sm82);
//        sl_8.add(sm83);
//        sl_8.add(sm84);

        //系统设置
//        sl_9.add(sm91);
        sl_9.add(sm92);
        sl_9.add(sm93);

        //店铺
        sl_10.add(sm101);


        menuList.add(sl_1);
        menuList.add(sl_2);
        menuList.add(sl_3);
        menuList.add(sl_4);
//        menuList.add(sl_5);
        menuList.add(sl_6);
//        menuList.add(sl_7);
//        menuList.add(sl_8);
        menuList.add(sl_9);
        menuList.add(sl_10);

        return menuList;
    }
}
